package com.y.simple.utils;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;

public class ImgCodeUtils {

    /**
     * <h1>用于在服务端存放验证码</h1>
     */
    public static final ThreadLocal<String> userThreadLocal = new ThreadLocal<>();


    /**
     * <h1>生成默认的base64图片信息</h1>
     * <h3>getCode() 获得验证码</h3>
     * <h3>getImageBase64() 获得图片Base64</h3>
     * @return LineCaptcha
     */
    public static LineCaptcha createBase64() {
        // 设置参数
        RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(130, 40);
        lineCaptcha.setGenerator(randomGenerator);
        // 重新生成code
        lineCaptcha.createCode();
        return lineCaptcha;
    }




    public static void add(String code){
        userThreadLocal.set(code);
    }

    public static String get(){
        return userThreadLocal.get();
    }

    public static void remove(){
        userThreadLocal.remove();
    }

}
